<?php
/**
 * 学生控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Employee extends Controller_BaseController {

	public $template = 'edu/template';

	public function action_index(){
		$params = array(
  			'title' => '职员管理',
  			'menu' => ''
  		);

		$where = array();
		if(\Input::get('notic', false)){
			$where['metadata.key'] = 'notices';
			$where['metadata.value'] = array('in', implode(',', \Input::get('notic')));
		}
		$params['items'] = \Model_Employee::getItems('*', $where, array('metadata'));

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/employee/items");
	}

	/**
	* 新增一个职员
	*
	* @return
	*/
	public function action_create(){

		if(\Input::method() == 'POST'){
			$data = \Input::post();
			$data['is_delete'] = 0;
			$data['seller_id'] = 1;

			/*$data['user_id'] = \Auth::create_user('employee' . time(), time(), isset($data['email']) && $data['email'] ? $data['email'] : time() . '@test.com', 1);

			\Model_People::forge($data)->save();*/

			$employee = \Model_Employee::forge($data);

			if($employee->save()){
				\Response::redirect('/admin/employee');
			}
		}
		
	}

	/**
	* 编辑职员资料
	*
	* @param $id int 职员ID
	* @return
	*/
	public function action_view($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$employee = \Model_Employee::find($id);
		if( ! $employee){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::is_ajax()){
			$employee->people;
			$emp = $employee->to_array();
			if(isset($employee->notices)){
				$emp['notices'] = $employee->notices;
			}
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $emp)));
		}

	}

	/**
	* 编辑职员资料
	*
	* @param $id int 职员ID
	* @return
	*/
	public function action_edit($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$data = \Input::post();

		$employee = \Model_Employee::find($id);
		if( ! $employee){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$people = \Model_People::find($employee->user_id);
		if($people){
			$people->set($data);
			$people->save();
		}		

		//清除不必要的数据
		unset($data['first_name']);
		unset($data['last_name']);
		unset($data['gender']);

		$data['setting_wechat'] = (int)$data['setting_wechat'];
		
		if(isset($data['notices'])){
			$employee->notices = $data['notices'];
		}

		$employee->set($data);

		if($employee->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
			}
			\Response::redirect('/admin/employee');
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
			}
		}
	}

	/**
	* 删除一个职员
	*
	* @param $id int 职员ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$employee = \Model_Employee::find($id);
		if( ! $employee){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$employee->is_delete = true;
		if($employee->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

}
